import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Window implements Observer{
	private final InputController	input;
	private JFrame frame = new JFrame();
	private JPanel quadPanel;
	private JLabel pingLabel = new JLabel("Ping: 	" + CopterInfo.getCI().getResponseTime() + " ms.");
	private JLabel stateLabel = new JLabel("State: 	" + CopterInfo.getCI().getState());
	private JLabel gyrosLabel = new JLabel("Gyros: 	" + CopterInfo.getCI().getRoll() + " ; " +
														CopterInfo.getCI().getPitch() + " ; " +
														CopterInfo.getCI().getYaw());

	public Window(){
		input = new InputController();
		frame.addKeyListener(input);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				frame.setVisible(false);
				frame.dispose();
				System.exit(0);
			}
		});
		
		ImageIcon img = new ImageIcon("");
		frame.setIconImage(img.getImage());
		
		quadPanel = new JPanel(new GridLayout(0,1,0,0));
		quadPanel.setPreferredSize(new Dimension(100,100));
		quadPanel.setBackground(Color.lightGray);
		quadPanel.add(pingLabel);
		quadPanel.add(stateLabel);
		quadPanel.add(gyrosLabel);
		
		frame.add(quadPanel);
		
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setSize(150, 100);
		frame.setVisible(true);
		
		CopterInfo.getCI().addObserver(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		pingLabel.setText("Ping: 	" + CopterInfo.getCI().getResponseTime() + " ms.");
		stateLabel.setText("State: 	" + CopterInfo.getCI().getState());
		gyrosLabel.setText("Gyros: 	" + CopterInfo.getCI().getRoll() + " ; " +
										CopterInfo.getCI().getPitch() + " ; " +
										CopterInfo.getCI().getYaw());
	}
}